<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
include_once( JIEQI_ROOT_PATH."/class/power.php" );
$power_handler =& jieqipowerhandler::getinstance( "JieqiPowerHandler" );
$power_handler->getsavedvars( "system" );
jieqi_checkpower( $jieqiPower['system']['adminuser'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
if ( empty( $_REQUEST['id'] ) )
{
		jieqi_printfail( LANG_NO_USER );
}
jieqi_loadlang( "users", JIEQI_MODULE_NAME );
$GLOBALS['_REQUEST']['id'] = intval( $_REQUEST['id'] );
include_once( JIEQI_ROOT_PATH."/class/users.php" );
$users_handler =& jieqiusershandler::getinstance( "JieqiUsersHandler" );
$user = $users_handler->get( $_REQUEST['id'] );
if ( !is_object( $user ) )
{
		jieqi_printfail( LANG_NO_USER );
}
if ( $user->getvar( "groupid" ) == JIEQI_GROUP_ADMIN && $jieqiUsersGroup != JIEQI_GROUP_ADMIN )
{
		jieqi_printfail( $jieqiLang['system']['cant_manage_admin'] );
}
if ( jieqi_checkpower( $jieqiPower['system']['deluser'], $jieqiUsersStatus, $jieqiUsersGroup, true, true ) )
{
		$adminlevel = 4;
}
else if ( jieqi_checkpower( $jieqiPower['system']['adminvip'], $jieqiUsersStatus, $jieqiUsersGroup, true, true ) )
{
		$adminlevel = 3;
}
else if ( jieqi_checkpower( $jieqiPower['system']['changegroup'], $jieqiUsersStatus, $jieqiUsersGroup, true, true ) )
{
		$adminlevel = 2;
}
else
{
		$adminlevel = 1;
}
if ( !isset( $_REQUEST['action'] ) )
{
		$GLOBALS['_REQUEST']['action'] = "edit";
}
switch ( $_REQUEST['action'] )
{
case "update" :
		$GLOBALS['_POST']['reason'] = trim( $_POST['reason'] );
		$GLOBALS['_POST']['pass'] = trim( $_POST['pass'] );
		$GLOBALS['_POST']['repass'] = trim( $_POST['repass'] );
		if ( strlen( $_POST['reason'] ) == 0 )
		{
				$errtext .= $jieqiLang['system']['change_user_reason']."<br />";
		}
		if ( $_POST['pass'] != $_POST['repass'] )
		{
				$errtext .= $jieqiLang['system']['password_not_equal']."<br />";
		}
		if ( empty( $errtext ) )
		{
				$log_fromdata = serialize( $user );
				if ( 4 <= $adminlevel && isset( $_POST['deluser'] ) && $_POST['deluser'] == 1 )
				{
						$GLOBALS['_REQUEST']['uid'] = $user->getvar( "uid" );
						$GLOBALS['_REQUEST']['jumpurl'] = JIEQI_URL."/admin/users.php";
						jieqi_useraction( "delete", $_REQUEST );
						exit( );
				}
				$GLOBALS['_REQUEST']['uid'] = $user->getvar( "uid" );
				$GLOBALS['_REQUEST']['jumpurl'] = JIEQI_URL."/admin/users.php";
				jieqi_useraction( "edit", $_REQUEST );
				exit( );
		}
		jieqi_printfail( $errtext );
		break;
case "edit" :
		include_once( JIEQI_ROOT_PATH."/admin/header.php" );
		include_once( JIEQI_ROOT_PATH."/lib/html/formloader.php" );
		$edit_form = new jieqithemeform( $jieqiLang['system']['user_manage'], "usermanage", JIEQI_URL."/admin/usermanage.php" );
		$edit_form->addelement( new jieqiformlabel( $jieqiLang['system']['table_users_uname'], $user->getvar( "uname" ) ) );
		$pass = new jieqiformpassword( $jieqiLang['system']['table_users_pass'], "pass", 25, 20 );
		$pass->setdescription( $jieqiLang['system']['not_change_password'] );
		$edit_form->addelement( $pass );
		$edit_form->addelement( new jieqiformpassword( $jieqiLang['system']['confirm_password'], "repass", 25, 20 ) );
		if ( 2 <= $adminlevel )
		{
				$group_select = new jieqiformselect( $jieqiLang['system']['table_users_groupid'], "groupid", $user->getvar( "groupid", "e" ) );
				foreach ( $jieqiGroups as $key => $val )
				{
						$group_select->addoption( $key, $val );
				}
				$edit_form->addelement( $group_select, true );
		}
		$edit_form->addelement( new jieqiformtext( $jieqiLang['system']['table_users_experience'], "experience", 25, 11, $user->getvar( "experience", "e" ) ) );
		$edit_form->addelement( new jieqiformtext( $jieqiLang['system']['table_users_score'], "score", 25, 11, $user->getvar( "score", "e" ) ) );
		if ( 3 <= $adminlevel )
		{
				$edit_form->addelement( new jieqiformtext( JIEQI_EGOLD_NAME, "egold", 25, 11, $user->getvar( "egold", "e" ) ) );
				$edit_form->addelement( new jieqiformtext( $jieqiLang['system']['table_users_esilver'], "esilver", 25, 11, $user->getvar( "esilver", "e" ) ) );
				$isvip = new jieqiformradio( $jieqiLang['system']['table_users_isvip'], "isvip", $user->getvar( "isvip", "e" ) );
				$isvip->addoption( 0, $jieqiLang['system']['user_no_vip'] );
				$isvip->addoption( 1, $jieqiLang['system']['user_is_vip'] );
				$isvip->addoption( 2, $jieqiLang['system']['user_super_vip'] );
				$edit_form->addelement( $isvip );
		}
		if ( 4 <= $adminlevel )
		{
				$yesno = new jieqiformradio( $jieqiLang['system']['delete_user'], "deluser", 0 );
				$yesno->addoption( 0, LANG_NO );
				$yesno->addoption( 1, LANG_YES );
				$edit_form->addelement( $yesno );
		}
		$edit_form->addelement( new jieqiformtextarea( $jieqiLang['system']['user_change_reason'], "reason", "", 6, 60 ), true );
		$edit_form->addelement( new jieqiformhidden( "action", "update" ) );
		$edit_form->addelement( new jieqiformhidden( "id", $_REQUEST['id'] ) );
		$edit_form->addelement( new jieqiformbutton( "&nbsp;", "submit", $jieqiLang['system']['user_save_change'], "submit" ) );
		$jieqiTpl->assign( "jieqi_contents", "<br />".$edit_form->render( JIEQI_FORM_MIDDLE )."<br />" );
		include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
}
?>
